public class Solution474 {
    public int findMaxForm(String[] strs, int m, int n) {
        int[][] count=new int[strs.length][2];
        for (int i=0;i<strs.length;i++) {
            String str=strs[i];
            char[] chars=str.toCharArray();
            for (char aChar : chars) {
                if (aChar=='0'){
                    count[i][0]++;
                }else{
                    count[i][1]++;
                }
            }
        }
        int[][][] f=new int[2][m+1][n+1];
        for (int i=0;i< strs.length;i++){
            for (int j=0;j<=m;j++){
                for (int k=0;k<=n;k++){
                    if (j>=count[i][0] && k>=count[i][1]) {
                        f[(i+1)%2][j][k]=Math.max(f[i%2][j][k],f[i%2][j-count[i][0]][k-count[i][1]]+1);
                    }else{
                        f[(i+1)%2][j][k]=f[i%2][j][k];
                    }
                }
            }
        }
        return f[strs.length%2][m][n];
    }

    public static void main(String[] args) {
        System.out.println(new Solution474().findMaxForm(new String[]{"10", "0001", "111001", "1", "0"},5,3));
    }
}
